package com.lancoo.answer.ui.child.training;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lancoo.answer.R;
import com.lancoo.answer.model.AnswerConstant;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Ques;
import com.lancoo.answer.model.bean.Type;
import com.lancoo.answer.widget.combinationView.BriefAnswerView;
import com.lancoo.answer.widget.combinationView.QueseBodyView;
import com.rxjava.rxlife.RxLife;

import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;

/**
 * Created by pengmi on 2021/12/1 20:53
 * Description:简答题作答view(翻译题，复合题主观题)
 */
public class ChildBriefAnswerFragment extends Fragment {

    private BriefAnswerView briefAnswerView;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.ev_fragment_brief_answer_quese, container, false);
    }


    /**
     *
     * @param kindIndex List<Type>题型下标
     * @param queseIndex List<Type>大题下标
     * @param smallQueseIndex List<Type>小题下标
     * @param answerType 作答类型 0-识别成英文，1-识别成中文
     * @return fragment
     */
    public static Fragment loadChildFragment(int kindIndex, int queseIndex,
                                             int smallQueseIndex, int answerType){
//        return ChildBriefListAnswerFragment.loadChildFragment(kindIndex, queseIndex, smallQueseIndex,answerType);
        ChildBriefAnswerFragment queseFragment=new ChildBriefAnswerFragment();
        Bundle mBundle=new Bundle();
        mBundle.putInt(AnswerConstant.KEY_TYPEINDEX,kindIndex);
        mBundle.putInt(AnswerConstant.KEY_QUESINDEX,queseIndex);
        mBundle.putInt(AnswerConstant.KEY_CHILDINDEX,smallQueseIndex);
        mBundle.putInt(AnswerConstant.KEY_ANSWERTYPE,answerType);
        queseFragment.setArguments(mBundle);
        return queseFragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View view){

        if(getArguments()==null){
            return;
        }
        int typeIndex=getArguments().getInt(AnswerConstant.KEY_TYPEINDEX);
        int queseIndex=getArguments().getInt(AnswerConstant.KEY_QUESINDEX);
        int childIndex=getArguments().getInt(AnswerConstant.KEY_CHILDINDEX);
        if(ConstantBean.Companion.getConstantExamBean()==null){
            return;
        }
        List<Type> typeList = ConstantBean.Companion.getConstantExamBean().getTypeList();
        Log.e("eee","听力简答进来了。。。");
        if(typeList==null||typeList.size()<=typeIndex){
            return;
        }
        Type type = typeList.get(typeIndex);
        List<Ques> quesList = type.getQuesList();
        if(quesList==null||quesList.size()<=queseIndex){
            return;
        }
        Ques ques = quesList.get(queseIndex);
        Child child = ques.getChildList().get(childIndex);
        QueseBodyView tv_queseIndex=view.findViewById(R.id.quese_body_view);
        tv_queseIndex.initData(child);
        briefAnswerView = view.findViewById(R.id.bref_answer_view);
        briefAnswerView.intData(child.getItemList().get(0),child.getChildAnswerType());
        briefAnswerView.setChildAnswerType(child.getChildAnswerType());
        briefAnswerView.setDistinguishType(getArguments().getInt(AnswerConstant.KEY_ANSWERTYPE));
        Log.e("eee","听力简答进来了。。。");
        briefAnswerView.setOpenMicroponeAnswer(!(ques.getAudioList()!=null&&ques.getAudioList().size()>0));
    }

    public void updateKeyboardShowState(boolean isShow){
        if(!isShow){
            return;
        }
        Single.timer(100, TimeUnit.MILLISECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .as(RxLife.as(this))
                .subscribe(aLong -> {
                    if(briefAnswerView!=null){
                        briefAnswerView.requestFocus();
                    }
                });

    }

    /**
     * 关闭音频作答
     */
    public void closeAudioAnswer(){
        if(briefAnswerView==null){
            return;
        }
        briefAnswerView.closeAudioAnswer();
    }

    @Override
    public void onResume() {
        super.onResume();
//        if(getActivity()!=null){
//            SoftInputUtils.hideSoftInput(getActivity());
//        }
    }

    @Override
    public void onPause() {
        super.onPause();
//        if(getActivity()!=null){
//            SoftInputUtils.hideSoftInput(getActivity());
//        }
    }

    public void adjustFontSize(){
        View view = getView();
        if(view==null){
            return;
        }
        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.adjustFontSize();
        BriefAnswerView briefAnswerView=view.findViewById(R.id.bref_answer_view);
        briefAnswerView.adjustFontSize();
    }

    public void markQuestion(){
        View view = getView();
        if(view==null){
            return;
        }
        QueseBodyView queseBodyView=view.findViewById(R.id.quese_body_view);
        queseBodyView.markQuestion();
    }
}
